from django.http import JsonResponse
from django.shortcuts import render
from .utils import gegtPublicData, getCenterLeftData, getBottomLeftData, getCenterRightData, getCenterRightChangeData, \
    getBottomRightData
from .utils import getCenterData
from .utils.getCenterData import getBaseData, getRollList
from .utils.getBottomLeftData import getSquareData
from .utils.getCenterRightData import getPriceSortData
from .utils.getCenterRightChangeData import getCircleData
from .utils.getBottomRightData import getRankData

from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.request import Request

# Create your views here.

#center 页面
def center(request):
    if request.method == 'GET':
        # 获取基础数据
        base_data = getBaseData()
        sumCar = base_data['sumCar']
        highVolume = base_data['highVolume']
        topCar = base_data['topCar']
        mostModel = base_data['mostModel']
        mostBrand = base_data['mostBrand']
        averagePrice = base_data['averagePrice']
        # oilRate = base_data['oilRate']
        # electricRate = base_data['electricRate']
        # mixRate = base_data['mixRate']
        # 获取轮播列表数据
        lastSortList = list(getRollList())  # 将字典转换为列表的元组形式
        oilRate, electricRate, mixRate = getCenterData.getTypeRate()
        return JsonResponse({
            'sumCar': sumCar,
            'highVolume': highVolume,
            'topCar': topCar,
            'mostModel': mostModel,
            'mostBrand': mostBrand,
            'averagePrice': averagePrice,
            'lastSortList': lastSortList,
            'oilRate':oilRate,
            'electricRate':electricRate,
            'mixRate':mixRate
        })
#centerLeft页面
def centerLeft(request):
    if request.method == 'GET':
        lastPieList = getCenterLeftData.getPieBrandData()
        return JsonResponse({
            'lastPieList':lastPieList
        })

#bottomLeft页面
def bottomLeft(request):
    if request.method == 'GET':
        brandList, saleList, priceList = getBottomLeftData.getSquareData()
        return JsonResponse({
            'brandList':brandList,
            'saleList':saleList,
            'priceList':priceList
        })

#centerRight页面
def centerRight(request):
        if request.method == 'GET':
            realData =  getCenterRightData.getPriceSortData()
            return JsonResponse({
                    'realData':realData
            })

#centerRightChange效果
def centerRightChange(request,energyType):
        if request.method == 'GET':
            oilData, electricityData = getCenterRightChangeData.getCircleData()
            realData = []
            if energyType == 1:
                realData = oilData
            else:
                realData = electricityData
            return JsonResponse({
                'realData':realData
            })

#bottomRight页面
def bottomRight(request):
    if request.method == 'GET':
        carData = getBottomRightData.getRankData()
        return JsonResponse({
            'carData':carData
        })

#登录
# class login(viewsets.ViewSet):
#     # 只有两个参数，默认路由后缀为方法名，可以添加第三个参数url_path='login'指定
#     @action(methods=['post'], detail=False)
#     def login(self, request):
#         # 对象使用.获取，字典使用['key']获取
#         password = user.objects.filter(username=request.data['username']).first().password
#         result = {
#             "code": 200,
#             "msg": "登录成功",
#             "body": ""
#         }
#         if password == request.data['password']:
#             return Response(result)
#         else:
#             result['msg'] = "登陆失败"
#             result['code'] = -1
#             return Response(result)
#
#     @action(methods=['post'], detail=False)
#     def register(self, request):
#         # username = request.data['username']
#         # password = request.data['password']
#         # User.objects.create(username=username, password=password)
#         # result = {
#         #     "code": 200,
#         #     "msg": "注册成功",
#         #     "body": ""
#         # }
#         return Response("shibai")

